﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

using permit.App_Code.Entity;
using permit.App_Code.DataAccess;


namespace permit.assgin
{
    public partial class AssginManager : System.Web.UI.Page
    {
        UserAccess userAcc = new UserAccess();
        AssginAccess assAcc = new AssginAccess();
        DepartmentAccess depAcc = new DepartmentAccess();
        DataSet ds;
        DataTable dt;


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                AssginAccess assAcc = new AssginAccess();
                UserTable userAu = (UserTable)Session["UserTable"];
                if (!assAcc.IsAssgin(userAu.USER_ID))
                {
                    Response.Redirect("~/main/Main.aspx?Sturt=Assgin");
                }


                gridAssBind();
                if (Request.QueryString["Sturt"] == "Edit")
                {
                    panelAss.Visible = true;
                    //绑定checkboxlist
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[0].ToString() == Request.QueryString["Name"])
                        {
                            lblUserName.Text = dr[0].ToString();
                            for (int index = 1; index < dt.Columns.Count - 1; index++)
                            {
                                if (Convert.ToInt32(dr[index]) == 1)
                                {
                                    cblAssgin.Items[index - 1].Selected = true;
                                }
                                //当部门无打印权限时，Assgin表中有打印权限时，去掉该权限。
                                if (index == 6 && Convert.ToInt32(dr[index]) == 1)
                                {

                                    if (depAcc.GetDepartmentPrint(dr[0].ToString()) == 1)
                                    {
                                        cblAssgin.Items[index - 1].Selected = false;
                                        assAcc.DeleteAssgin(Convert.ToInt32(dr["user_ID"]), index);
                                    }
                                }
                            }
                            break;
                        }

                    }
                }
                else
                {
                    panelAss.Visible = false;
                }

            }

        }

        public void gridAssBind()
        {
            dt = getAssDataTable();
            gridAssgin.DataSource = dt;
            if (Session["Index"] != null)
            {
                gridAssgin.PageIndex = Convert.ToInt32(Session["Index"]);
            }
            gridAssgin.DataBind();
        }

        protected void gridAssgin_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断ROOMID是不是数字
            Regex r = new Regex(@"^\d+(\.)?\d*$");

            int index = 0;
            foreach (TableCell tc in e.Row.Cells)
            {
                if (r.IsMatch(tc.Text) && index % e.Row.Cells.Count > 0)
                {

                    Image image = new Image();

                    if (tc.Text == "0")
                    {

                        image.ImageUrl = @"~/assgin/Images/wrong.gif";
                        tc.Controls.Add(image);


                    }
                    else
                    {
                        image.ImageUrl = @"~/assgin/Images/right.gif";
                        tc.Controls.Add(image);
                    }
                }
                index++;
            }
        }

        protected void cblAssgin_SelectedIndexChanged(object sender, EventArgs e)
        {
            dt = getAssDataTable();
            
            for (int index = 0; index < cblAssgin.Items.Count; index++)  // 遍历checkbox
            {
                if (cblAssgin.Items[index].Selected)  //判断是否被选中
                {
                    foreach (DataRow dr in dt.Rows)  // 遍历datatable
                    {
                        if (lblUserName.Text == dr["user_Name"].ToString())  // 用户名相同
                        {
                            if (Convert.ToInt32(dr[index + 1]) == 0)  //对应项之前是否被选中，0表示没有
                            {
                                if (index == 5 && depAcc.GetDepartmentPrint(dr["user_Name"].ToString()) == 1) //判断是否有打印权限 1表示没有
                                {

                                    cblAssgin.Items[index].Selected = false;
                                    Page.RegisterStartupScript("this","<script>alert('该用户所在单位无打印权限！')</script>");
                                }
                                else //有打印权限 并且不是打印项
                                {
                                    assAcc.InsertAssgin(Convert.ToInt32(dr["user_ID"]), index + 1); //项数据库表中插入值
                                    break;
                                }
                            }
                            //else  //之前又被选中
                            //{
                            //    assAcc.InsertAssgin(Convert.ToInt32(dr["user_ID"]), index + 1);
                            //}
                        }
                    }
                }
                else  //checkBox 该项没有被选中
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (lblUserName.Text == dr["user_Name"].ToString())
                        {
                            if (Convert.ToInt32(dr[index + 1]) == 1)  //如果之前又被选中
                            {
                                assAcc.DeleteAssgin(Convert.ToInt32(dr["user_ID"]), index + 1);
                                break;
                            }
                        }
                    }
                }
            }

            gridAssBind();
        }

        private DataTable getAssDataTable()
        {

            DataSet ds = userAcc.GetUsers();

            dt = new DataTable();

            dt.Columns.Add(new DataColumn("user_Name", typeof(string)));
            dt.Columns.Add(new DataColumn("iPermit", typeof(int)));
            dt.Columns.Add(new DataColumn("employee", typeof(int)));
            dt.Columns.Add(new DataColumn("department", typeof(int)));
            dt.Columns.Add(new DataColumn("assgin", typeof(int)));
            dt.Columns.Add(new DataColumn("ePermit", typeof(int)));
            dt.Columns.Add(new DataColumn("pPermit", typeof(int)));

            dt.Columns.Add(new DataColumn("user_ID", typeof(int)));
            foreach (DataRow dataRow in ds.Tables[0].Rows)
            {
                DataRow dr = dt.NewRow();
                if (((UserTable)Session["UserTable"]).DEP_ID == 17) // 对应市局的dep_ID号
                {
                    dr["user_Name"] = dataRow["user_Name"];
                    DataSet dsAss = assAcc.GetAu_IDs(Convert.ToInt32(dataRow["user_ID"].ToString()));

                    for (int index = 1; index < dt.Columns.Count; index++)
                    {
                        dr[index] = 0;
                    }

                    foreach (DataRow row in dsAss.Tables[0].Rows)
                    {
                        dr[Convert.ToInt32(row["au_ID"])] = 1;
                    }


                    dr["user_ID"] = dataRow["user_ID"];
                    dt.Rows.Add(dr);
                }
                else
                {
                    if (Convert.ToInt32(dataRow["dep_ID"]) == ((UserTable)Session["UserTable"]).DEP_ID) //判断自己部门下的员工
                    {
                        dr["user_Name"] = dataRow["user_Name"];
                        DataSet dsAss = assAcc.GetAu_IDs(Convert.ToInt32(dataRow["user_ID"].ToString()));

                        for (int index = 1; index < dt.Columns.Count; index++)
                        {
                            dr[index] = 0;
                        }

                        foreach (DataRow row in dsAss.Tables[0].Rows)
                        {
                            dr[Convert.ToInt32(row["au_ID"])] = 1;
                        }

                        dr["user_ID"] = dataRow["user_ID"];
                        dt.Rows.Add(dr);
                    }
                }
               
            }
            return dt;
        }

        protected void gridAssgin_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Session["Index"] = e.NewPageIndex;
            gridAssgin.PageIndex = e.NewPageIndex;
            gridAssBind();
        }
    }
}
